writeLines("************************************************************************************************************************")

# 	Filename: jurisdiction_driver.R
# 	Description: Driver script to produce analyses where "ratifyrome" is the dependent variable.
writeLines("Description: Driver script to produce analyses where ratifyrome is the dependent variable.")
# 	Author: Barry Hashimoto
# 	Date: December 2019
# 	For: Barry Hashimoto, "Autocratic Consent to International Law: the Case of the International Criminal Court's Jurisdiction," International Organization.

timestamp()
set.seed(100)
options(scipen=999) # Prevent scientific formatting of numbers.

suppressPackageStartupMessages({library(Amelia); library(Zelig); library(brglm); library(MatchIt); library(cem); library(doBy); library(mvtnorm); library(mvnfast); library(plyr); library(mitools); library(lattice); library(caret); library(memisc); library(parallel); library(future)})

writeLines(""); writeLines("") 		
writeLines("----------------------------------------------------------------------------------------------------------------------")
writeLines("Part I: Analyze the consent to ICC jurisdiction of all states using the multiply imputed data sets.")

  rm(list=ls())
  load("imputedicc.RData") # Saved multiply imputed data set.
   for (z in 1:10){full$imputations[[z]]$ruleoflaw <- full$imputations[[z]]$vdem.ruleoflaw} # VDEM sourced rule of law variable is renamed to distinguish it.
   for (z in 1:10){full$imputations[[z]] <- full$imputations[[z]][is.na(full$imputations[[z]]$ratifyrome)==F,]} # Drop all post-ratification observations.
  # Calculate country-specific time-trends estimated from cubic polynomial. Use of one data set suffices since there are no NAs.
    set <- full$imputations[[1]]; set$ccode <- factor(set$ccode)
    ratify.trend.country <- predict(lm(ratifyrome ~ ccode*poly(quartersinoffice.since1998Q3, 3), data = set))
    for (z in 1:10){full$imputations[[z]] <- data.frame(full$imputations[[z]], ratify.trend.country)} 
  # Calculate a single time trend estimated from cubic polynomial
    for (z in 1:10){full$imputations[[z]]$ratify.trend.single <- predict(lm(ratifyrome ~ poly(quartersinoffice.since1998Q3, 3), data = full$imputations[[z]]))} 
  # How many unique states and leaders in the data set to analyze?
    length(unique(full$imputations[[1]]$lcode)); length(unique(full$imputations[[1]]$ccode)) 
  # Run entire script with statistical analysis.
    source("jurisdictionMI_source.R", echo = F, spaced = T)

writeLines(""); writeLines("")    
writeLines("----------------------------------------------------------------------------------------------------------------------")
writeLines("Part II: Analyze consent to ICC jurisdiction by only the capital-eligible states using the multiply imputed data sets.")
 
  rm(list=ls())
  load("imputedicc.RData") # Saved multiply imputed data set.
   for (z in 1:10){full$imputations[[z]]$ruleoflaw <- full$imputations[[z]]$vdem.ruleoflaw} # VDEM sourced rule of law variable is renamed to distinguish it.
   for (z in 1:10){full$imputations[[z]] <- full$imputations[[z]][is.na(full$imputations[[z]]$ratifyrome)==F,]} # Drop all post-ratification observations.
  # Calculate country-specific time-trends estimated from cubic polynomial. Use of one data set suffices since there are no NAs.
    set <- full$imputations[[1]]; set$ccode <- factor(set$ccode)
    ratify.trend.country <- predict(lm(ratifyrome ~ ccode*poly(quartersinoffice.since1998Q3, 3), data = set))
    for (z in 1:10){full$imputations[[z]] <- data.frame(full$imputations[[z]], ratify.trend.country)} 
  # Calculate a single time trend estimated from cubic polynomial
    for (z in 1:10){full$imputations[[z]]$ratify.trend.single <- predict(lm(ratifyrome ~ poly(quartersinoffice.since1998Q3, 3), data = full$imputations[[z]]))} 
  # Keep only aid-eligible states.
    mean.aid <- summaryBy(capital.scaled ~ ccode, data = full$imputations[[1]], FUN = mean)
    # unique(mean.aid[mean.aid$capital.scaled>0, 1]) # Uncomment to get number of included states.
     for(z in 1:10){
	    mean.aid <- summaryBy(capital.scaled ~ ccode, data = full$imputations[[1]], FUN = mean)
	    aid.eligible.states <- mean.aid[mean.aid$capital.scaled>0, 1]
	    full$imputations[[z]] <- full$imputations[[z]][which(full$imputations[[z]]$ccode %in% aid.eligible.states),]
	    }	    
  # How many unique states and leaders in the data set to analyze?
    length(unique(full$imputations[[1]]$lcode)); length(unique(full$imputations[[1]]$ccode))   
  # Run script with statistical analysis for the unimputed data.
    source("jurisdictionMI_source.R", echo = F, spaced = T) 

writeLines(""); writeLines("")    
writeLines("----------------------------------------------------------------------------------------------------------------------")
writeLines("Part III:  Analyze all states' consent to ICC jurisdiction using the unimputed data set, applying listwise deletion.")

  rm(list=ls())
  load("iccdata.RData") # Saved unimputed data set.
  # Run script with statistical analysis for the unimputed data.
  source("jurisdictionLD_source.R", echo = F, spaced = T)

writeLines(""); writeLines("")
writeLines("----------------------------------------------------------------------------------------------------------------------")
 writeLines("End of jurisdiction_driver.R script")
 timestamp()
 writeLines(""); writeLines("")
writeLines("************************************************************************************************************************")

